import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.naming.NamingException;

import unilearn.domain.*;
import unilearn.facade.*;

/**
 * @author Yin Zhang, 5393558
 * @version 0.1, May 10 2008, initial creation
 */
public class StaffUserStory6
{
	private static PrintWriter fileOutput = null;
	
	private static final SimpleDateFormat DATEFORMAT = new SimpleDateFormat("dd/MM/yyyy 'at' HH:mm:ss");
	private static final String SEPARATELINE = "--------------------------------------------------------";
	private static final String OUTPUTPATH = "staffUserStory6";
	private static final String OUTPUTFILENAME = "staffGrantExtension.txt";
	private static final long ONEDAY = (long) 1000.0 * 60 * 60 * 24;
	
	public StaffUserStory6() throws FileNotFoundException
	{
		File filePath = new File(OUTPUTPATH);
		
		if(!filePath.exists())
		{
			filePath.mkdir();
		}
		
		fileOutput = new PrintWriter(new FileOutputStream(OUTPUTPATH + File.separator + OUTPUTFILENAME));
	}
	
	public void createNewExtension(String[] extensionDetails) throws SQLException, NamingException, IOException
	{
		Extension extension = new Extension();
		long assignmentID = Long.parseLong(extensionDetails[0]);
		String studentID = extensionDetails[1];
		extension.setAssignmentID(assignmentID);
		extension.setStudentID(studentID);
		extension.setExtensionName(extensionDetails[2]);
		extension.setExtensionDescription(extensionDetails[3]);
		java.util.Date today = new java.util.Date();
		java.util.Date tomorrow = new java.util.Date(System.currentTimeMillis() + ONEDAY);
		Timestamp grantedDate = new Timestamp(today.getTime());
		Timestamp finishedDate = new Timestamp(tomorrow.getTime());
		extension.setExtensionGrantedDate(grantedDate);
		extension.setExtensionFinishedDate(finishedDate);
		extension.setExtensionGrantedBy(extensionDetails[4]);
		
		ExtensionFacade.createNewExtension(extension);
		
		fileOutput.println("Extension granted for student " + studentID + ", Assignment " + assignmentID);
		fileOutput.println(SEPARATELINE);
		printAssignment(assignmentID, studentID);
	}
	
	private void printAssignment(long assignmentID, String studentID) throws SQLException, NamingException, IOException
	{
		Assignment assignment = AssignmentFacade.getAssignmentByID(assignmentID);
		Subject subject = SubjectFacade.getSubjectByID(assignment.getSubjectID());
		String subjectName = subject.getSubjectCode() + " - " + subject.getSubjectName();
		String fileName = "None";
		long fileSize = 0;
		String releaseDate = formatDate(assignment.getReleaseDate());
		String dueDate = formatDate(checkExtension(assignmentID, studentID, assignment.getDueDate()));
		String accStatus = null;
		if(assignment.getFileID() != -1)
		{
			DataFile file = FileFacade.getFileByID(assignment.getFileID());
			fileName = file.getFileName();
			fileSize = file.getFileSize();
		}
		
		if(assignment.getAcceptSubmissions())
		{
			if(assignment.getAcceptMarking())
			{
				accStatus = "open for marking and submission";
			}
			else
			{
				accStatus = "open for submission";
			}
		}
		else
		{
			if(assignment.getAcceptMarking())
			{
				accStatus = "open for marking";
			}
			else
			{
				accStatus = "closed for submission";
			}		
		}
		
		fileOutput.println(subjectName);
		fileOutput.println("Assignment Name: \t\t\t\t\t" + assignment.getName());
		fileOutput.println("Release Date: \t\t\t\t\t\t" + releaseDate);
		fileOutput.println("Due Date: \t\t\t\t\t\t\t\t" + dueDate);
		fileOutput.println("Assignment status: \t\t\t\t" + accStatus);
		fileOutput.println("Assignment Files: \t\t\t\t" + fileName + " - " + fileSize + " bytes");
		fileOutput.println("Assignment Description: \t" + assignment.getDesc());
	}
	
	private Timestamp checkExtension(long assignmentID, String studentID, Timestamp dueDate) throws SQLException, NamingException
	{
		Extension extension = ExtensionFacade.getExtensionByStudentAndAssignment(assignmentID, studentID);
		Timestamp extensionDate = null;
		
		if(extension != null)
		{
			extensionDate = extension.getExtensionFinishedDate();
			
			if(extensionDate.after(dueDate))
			{
				return extensionDate;
			}
			else
			{
				System.err.println("Due date is bigger than extension date");
			}
		}
		
		return dueDate;
	}
	
	private static String formatDate(Timestamp timestamp)
	{
        if (timestamp == null) 
        {
            return null;
        }
        else 
        {
            return DATEFORMAT.format((java.util.Date) timestamp);
        }
	}
	
	public void close()
	{
		try
		{
			fileOutput.close();
		}
		finally
		{
			fileOutput = null;
		}
	}
}